package com.grape.api.v1;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grape.domain.Category;
import com.grape.service.ICategoryService;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.dto.CategoryBudgetDTO;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.grape.annotation.RepeatSubmit;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 分类表 控制器
 * @author      putao
 * @email       2628279194@qq.com
 */
@RestController
@RequestMapping("categories")
@Api(tags = "分类接口")
@AllArgsConstructor
public class CategoryController extends BaseController<Category> {

    private final ICategoryService categoryService;


    @GetMapping
    @ApiOperation(value = "分类分页")
    @OperateMethodName(value = "分类表列表分页", url = "account:categories:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<Category> pageInfo = categoryService.pageList(baseVo);
        List<Category> categoryList = pageInfo.getList();
        return CollectionUtil.isNotEmpty(categoryList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @GetMapping("listPageCustom")
    @ApiOperation(value = "分类自定义分页")
    @OperateMethodName(value = "分类表列表", url = "account:categories:listPageCustom")
    public R<?> pageListCustom(BaseVo baseVo) {
        Page<Category> pages = PageHelper.startPage(baseVo.getPageNum(), baseVo.getPageSize());
        categoryService.pageListCustom(baseVo);
        PageInfo<Category> pageInfo = pages.toPageInfo();
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @RepeatSubmit
    @PutMapping
    @ApiOperation(value = "分类修改")
    @OperateMethodName(value = "分类表修改", url = "account:categories:update")
    public R<?> update(@Validated @RequestBody Category category) {
        if (!Strings.isNotBlank(category.getId())) {
            return updateNotFound(category, null);
        }
        categoryService.updateData(category);
        return updateSuccess(null, null);
    }


    @RepeatSubmit
    @PostMapping
    @ApiOperation(value = "添加分类")
    @OperateMethodName(value = "添加分类表", url = "account:categories:insert")
    public R<?> insert(@Validated @RequestBody Category category) {
        return insertSuccess(categoryService.insertData(category), null);
    }


    @DeleteMapping
    @ApiOperation(value = "删除分类，可批量多个id逗号拼接")
    @OperateMethodName(value = "分类表删除,批量", url = "account:categories:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空") @RequestParam String ids) {
        int number = categoryService.deleteData(ids);
        if (number < 1) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }


    @GetMapping("detail")
    @ApiOperation(value = "分类详情")
    @OperateMethodName(value = "分类表详情", url = "account:categories:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        Category category = categoryService.detail(id);
        if (category != null) {
            return selectSuccess(category, id);
        }
        return selectNotFound(id, null);
    }

    @GetMapping("listAll")
    @ApiOperation(value = "分类全部列表")
    @OperateMethodName(value = "分类全部列表", url = "account:categories:listAll")
    public R<?> listAll(String accountBookId) {
        List<Category> list = categoryService.listAll(accountBookId);
        return CollectionUtil.isNotEmpty(list) ? selectSuccess(list, null) : selectNotFound(list, null);
    }

    @GetMapping("statisticBudget")
    @ApiOperation(value = "按月分类预算统计")
    @OperateMethodName(value = "按月分类预算统计", url = "account:categories:statisticBudget")
    public R<?> statisticBudget(@NotBlank(message = "账本id不能为空") @RequestParam("accountBookId") String accountBookId) {
        List<CategoryBudgetDTO> list = categoryService.statisticBudget(accountBookId);
        return CollectionUtil.isNotEmpty(list) ? selectSuccess(list, null) : selectNotFound(list, null);
    }
}

